<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>添加管理员</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js"></script>
</head>
<body>
<%--<form action="/admin/admin" method="post" onsubmit="return validator()">--%>
<form id="adminForm">

    <input type="hidden" name="adminID" value="${admin.adminID}"><br>
    <input type="text" name="adminName" id="adminName" value="${admin.adminName}"><br>
    <input type="password" name="adminPwd" id="adminPwd" value="${admin.adminPwd}"><br>
    <input type="button" value="保存" id="btnSave">
    <%--</form>--%>
</form>
<script language="JavaScript">
    // function validator() {
    //     // var adminName=document.getElementById("adminName");
    //     var adminName=document.querySelector("#adminName");
    //
    //     var password=document.getElementById("password");
    //    if(!(adminName.value.length>0)) {
    //        alert("管理员名称不能为空")
    //        return false;
    //    }
    //     if(!(password.value.length>0)) {
    //         alert("密码不能为空")
    //         return false;
    //     }
    //     return true;
    // }
    function validator() {
        if ($("#adminName").val() == "") {
            alert("管理员名称不能为空")
            return false;
        }
    }

    // $("#btnSave").click(function () {
    //     // alert($("#adminForm").serialize())
    //     $.post("/admin/admin", $("#adminForm").serialize(), function (data, status) {
    //         if (data == 1) {
    //             window.location.href = "/admin/admin";
    //         } else {
    //             alert("操作失败")
    //         }
    //     })
    // })

    jQuery.fn.serializeObject = function () {
        var formData = {};
        var formArray = this.serializeArray();
        for(var i = 0, n = formArray.length; i < n; ++i){
            formData[formArray[i].name] = formArray[i].value;
        }
        return formData;
    };

    $("#btnSave").click(function () {
        var data = JSON.stringify($("#adminForm").serializeObject());
        $.post("/admin", data, function (data, status) {
            if (data == 1) {
                window.location.href = "/admin";
            } else {
                alert("操作失败")
            }
        })
    });

</script>
</body>
</html>
